﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.DAL;
using MINERP.MODEL;

namespace MINERP.BLL.InfoConfig
{
    public class ComplexProductBll
    {


        /// <summary>
        /// 获取所有的复合产品
        /// </summary>
        /// <returns></returns>
        public List<ComplexProductInfo> GetAllComplexProduct()
        {
            List<ComplexProductInfo> ListProduct = new List<ComplexProductInfo>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<Tb_Product> listP = db.Tb_Product.Where(p => p.IsComplex && p.Status == 1).ToList();
                foreach (Tb_Product porduct in listP)
                {
                    if (!porduct.Tb_PorductCategoryReference.IsLoaded)
                    {
                        porduct.Tb_PorductCategoryReference.Load();

                    }
                    ComplexProductInfo cpi = new ComplexProductInfo();
                    cpi.ProductId = porduct.ProuctId;
                    cpi.ProoductName = porduct.ProductName +"("+ porduct.Width + "x" + porduct.Height+")";
                    cpi.ProductDes = porduct.ProductDes;
                    cpi.ProductClassName = porduct.Tb_PorductCategory.ProductCategoryName;
                    cpi.SubProducts = new List<SubProductInfo>();
                    foreach (var sub in db.Sub_Product.Where(sp => sp.Tb_Product.ProuctId == porduct.ProuctId))
                    {
                        Tb_Product tp = db.Tb_Product.FirstOrDefault(stp => stp.ProuctId == sub.Containid);
                        if (!tp.Tb_PorductCategoryReference.IsLoaded)
                        {
                            tp.Tb_PorductCategoryReference.Load();
                        }
                        SubProductInfo spi = new SubProductInfo();
                        spi.Num = sub.Num;
                        spi.ContainProductid = sub.Containid;
                        spi.ProductClassName = tp.Tb_PorductCategory.ProductCategoryName;
                        spi.ProoductName = tp.ProductName + "(" + tp.Width + "x" + tp.Height+")" ;
                        spi.ProductId = sub.SubProductid;
                        spi.isAdd = false;
                        spi.isDelete = false;
                        spi.isOld = true;
                        spi.isSelect = true;
                        spi.isEdit = false;
                        
                        cpi.SubProducts.Add(spi);
                    }
                    ListProduct.Add(cpi);

                }
            }
            return ListProduct;

        }
        /// <summary>
        /// 获取所有子产品
        /// </summary>
        /// <returns></returns>
        public List<SubProductInfo> GetAllSubProduct()
        {
            List<SubProductInfo> Rtnlist = new List<SubProductInfo>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<Tb_Product> listP = db.Tb_Product.Where(p => p.IsComplex == false && p.Status == 1).ToList();
                foreach (Tb_Product tp in listP)
                {
                    SubProductInfo spi = new SubProductInfo();
                    spi.isAdd = false;
                    spi.isDelete = false;
                    spi.isOld = false;
                    spi.isSelect = false;
                    spi.isEdit = false;
                    spi.Num = 1;
                    tp.Tb_PorductCategoryReference.Load();
                    spi.ProductClassName = tp.Tb_PorductCategory.ProductCategoryName;
                    spi.ProoductName = tp.ProductName +"(" + tp.Width + "x" + tp.Height + ")";
                    spi.ProductId = tp.ProuctId;
                    spi.ContainProductid = 0;
                    Rtnlist.Add(spi);
                }
                return Rtnlist;
            }
        }

        public void UpdateProduct(List<ComplexProductInfo> inList)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                foreach (ComplexProductInfo cpi in inList)
                {
                    foreach (SubProductInfo spi in cpi.SubProducts)
                    {
                        if (spi.isAdd)
                        {
                            Sub_Product sp = new Sub_Product();
                            sp.Containid = spi.ProductId;//赋值时ProductId为product的ID
                            sp.Describe = "";
                            //sp.MainProductid = cpi.ProductId;
                            sp.Num = spi.Num;
                            Tb_Product tbProduct = db.Tb_Product.FirstOrDefault(p => p.ProuctId == cpi.ProductId);
                            sp.Tb_Product =tbProduct ;
                            Tb_Product tbConProduct = db.Tb_Product.FirstOrDefault(p => p.ProuctId == spi.ProductId);
                            sp.SubProductName = tbConProduct.ProductName;
                            db.AddToSub_Product(sp);
                        }
                        else if(spi.isDelete)
                        {
                            Sub_Product sp = db.Sub_Product.FirstOrDefault(p=>p.SubProductid==spi.ProductId);
                            db.DeleteObject(sp);
                        }
                        else if (spi.isEdit)
                        {
                            Sub_Product sp = db.Sub_Product.FirstOrDefault(p => p.SubProductid == spi.ProductId);
                            sp.Num = spi.Num;
                        }
                        else
                        {
                            //还是原来的
                        }
                    }

                }
                db.SaveChanges();
            }
        }

    }
    public class ComplexProductInfo
    {
        public int ProductId { get; set; }
        public string ProductClassName { get; set; }
        public string ProoductName { get; set; }
        public List<SubProductInfo> SubProducts { get; set; }
        public string ProductDes { get; set; }
        public bool IsEdit
        {
            get
            {
                if (SubProducts.Where(p => p.isAdd || p.isDelete || p.isEdit).Count() > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }
    public class SubProductInfo
    {
        //public delegate void SelectChangeActionHandel(SubProductInfo spi);
        //public event SelectChangeActionHandel SelectChangeAction;
        private bool _isSelect;
        private int _Num;
        public int ProductId { get; set; }
        public string ProductClassName { get; set; }
        public string ProoductName { get; set; }
        public int ContainProductid { get; set; }
        public bool isAdd { get; set; }
        public bool isDelete { get; set; }
        public bool isEdit { get; set; }
        public bool isOld { get; set; }
        public int Num
        {
            get { return _Num; }
            set
            {
                if (_Num != value)
                {
                    isEdit = true;
                }
                _Num = value;
            }
        }
        public bool isSelect
        {
            get { return _isSelect; }
            set
            {
                if (_isSelect != value)//表示值将改变
                {
                    if (value)//操作选中
                    {
                        if (isOld)//数据库中已经存在的
                        {
                            isAdd = false;
                        }
                        else
                        {
                            isAdd = true;
                            EventMonitor.SelectChange(this);
                        }

                    }
                    else//操作取消
                    {
                        if (isOld)
                        {
                            isDelete = true;
                        }
                        else
                        {
                            isDelete = false;
                            EventMonitor.CancelChange(this);
                        }
                    }

                }
                _isSelect = value;

            }
        }
    }
    public static class EventMonitor
    {
        public static EventHandler<MonitorEventArgs> MonitoringSelectChange;
        public static EventHandler<MonitorEventArgs> MonitoringCancelChange;
        public static void SelectChange(SubProductInfo args)
        {
            if (null != MonitoringSelectChange)
            {
                MonitoringSelectChange(null, new MonitorEventArgs(args));
            }
        }
        public static void CancelChange(SubProductInfo args)
        {
            if (null != MonitoringSelectChange)
            {
                MonitoringSelectChange(null, new MonitorEventArgs(args));
            }
        }
    }
    public class MonitorEventArgs : EventArgs
    {
        public SubProductInfo args { get; private set; }
        public MonitorEventArgs(SubProductInfo inArgs)
        {
            this.args = inArgs;
        }

    }
}
